<?php

namespace server\dao;

use common\helpers\ConstantHelper;
use Yii;
use yii\mongodb\Query;

/**
 * This is the model class for collection "lane_devices".
 *
 * @property \MongoDB\BSON\ObjectID|string $_id
 */
class DevicesProcess extends \common\core\server\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function collectionName()
    {
        //return 'devices';
        return 'devices_process';
    }

    /**
     * {@inheritdoc}
     */
    public function attributes()
    {
        return [
            '_id',
            'ip',
            'process',
            'created_at',
            'updated_at'
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            '_id' => 'ID',
            'ip' => 'IP',
            'process' => 'Process',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }


    public function getAllProcess($isAll = true){
        $query = new Query();
        $query->from($this->collectionName());
        $query->select(['ip','process']);
        //return $query->createCommand()->getRawSql();
        return $isAll ? $query->all() : $this->mongodbPage($query);
    }

    public function getProcessByIP($ip)
    {
        $query = new Query();
        return $data = $query
            ->select(['ip', 'process'])
            ->where(['ip' => $ip])
            ->from($this->collectionName())
            ->one();
    }

    public function updateDevicesProcess($params)
    {
        $collection = \Yii::$app->mongodb->getCollection($this->collectionName());
        //先移除
        $collection->remove(['ip' => $params['ip']]);
        //再添加
        return $collection->insert($params);
    }

}
